package com.syx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syx.entity.Pro;
import com.syx.entity.User;
import com.syx.service.ProService;
import com.syx.util.FileUploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ProController {
    @Resource
    private ProService proService;

    //登录  注释  
    @RequestMapping("login.do")
    public String login(User user, HttpSession session){
        User user1 = proService.login(user);
        if (user1!=null){
            session.setAttribute("user1",user1);
            return "redirect:list.do";
        }
        return "redirect:index.jsp";
    }

    //列表
    @RequestMapping("list.do")
    public String proList(Model model,
                          String pname,
                          @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        Map map = new HashMap();
        map.put("pname",pname);
        List list = proService.proList(map);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }

    //添加中转
    @RequestMapping("addTo.do")
    public String addTo(){
        return "add";
    }

    //动态复选
    @RequestMapping("baseList.do")
    @ResponseBody
    public Object baseList(){
        List list = proService.baseList();
        return list;
    }

    //添加
    @RequestMapping("add.do")
    @ResponseBody
    public Object add(Pro pro, Integer[] bids, MultipartFile photo,HttpSession session){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            pro.setPic(upload);
        }
        int i = proService.add(pro,bids);
        return i;
    }
}
